package com.frogs.ucenter.v1.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.frogs.framework.base.web.BaseController3;
import com.frogs.system.web.api.response.FailResponse;
import com.frogs.system.web.api.response.Response;
import com.frogs.system.web.exception.AppBaseException;
import com.frogs.ucenter.entity.UserProtection;
import com.frogs.ucenter.v1.request.UserProtectionQueryRequest;
import com.frogs.ucenter.v1.request.UserProtectionSaveRequest;
import com.frogs.ucenter.v1.service.UserProtectionService;
import com.frogs.ucenter.v1.util.ApiConstants;

@Controller(ApiConstants.VERSION + "userProtectionController")
@RequestMapping("/" + ApiConstants.VERSION )
public class UserProtectionController extends BaseController3 {

    @Resource(name= ApiConstants.VERSION + "userProtectionService")
    private UserProtectionService userProtectionService;

	private static final String ROOT = "/user/question";
    private static final String USER_QUESTION_DETAIL = ROOT + DETAIL;
    
    private static final String USER_QUESTION_MERGE = ROOT + MERGE;
    
    /**
     * 接口说明：新增和更新在同一个入口，程序根据id判断原先是否存在，若存在则更新，不存在则插入
     * @param httpRequest
     * @param userPro
     * @return
     */
    @RequestMapping(USER_QUESTION_MERGE)
    public Response userQuestionMerge(HttpServletRequest httpRequest,UserProtection userPro){
    	UserProtectionSaveRequest request = new UserProtectionSaveRequest();
    	Response response;
    	try{
    		request.parse(httpRequest);
    		response = userProtectionService.saveProtection(request);
    		response.setTip("保存密保问题成功");
    	}catch(Exception e) {
    		response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
    	
    	return response;
    }
    
    @RequestMapping({USER_QUESTION_DETAIL})
    Response userQuestionDetail(HttpServletRequest httpRequest){
    	UserProtectionQueryRequest request = new UserProtectionQueryRequest();
    	Response response;
    	try {
    		request.parse(httpRequest);
    		response = userProtectionService.getProtection(request);
    	} catch(Exception e) {
    		response = new FailResponse();
			if (e instanceof AppBaseException) {
				response.setTip(e.getMessage());
			} else {
				response.setTip("errro message" + e.getMessage());
			}
		}
    	
    	return response;
    }
}